package com.android.vspice;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TableLayout;
import android.widget.TableRow;

import com.android.vspice.swig.VSpice;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;

public class GraphResult extends Activity {
		
	GraphView graphView;
	GraphViewData[] graphData;
	String[] strName = {"Vsrc", "R", "C", "V#Branch", "L"};
	PopupWindow popup;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.graphresult);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		
		//
		VSpice.test();
		int length = VSpice.GetNumData();
		double[] data = new double[length];
		//String[] name = VSpice.GetVarName(0);
		
		graphData = new GraphViewData[length];
		for(int i = 0 ; i < length ; i++) {
			graphData[i] = new GraphViewData(i, Math.sin(0.1 * i));
		}
		
		graphView = new GraphView(this, graphData, "Title", null, null);
		graphView.setScalable(true);
		graphView.setScrollable(true);
		LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout_graph);
		linearLayout.addView(graphView);
		
		TableLayout table = (TableLayout)findViewById(R.id.tableLayout);
		makeButtonTable(table, strName);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}	
	
	private void makeButtonTable(TableLayout layout, String[] strText) {
		final int COLUMNS = 4;
		TableRow tableRow;
		
		if(strText.length == 0)
			return;
		
		int rows = strText.length / COLUMNS;
		
		for(int i = 0 ; i <= rows ; i++) {
			tableRow = new TableRow(this);
			tableRow.setLayoutParams(new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			
			for(int j = 0 ; j < COLUMNS && i * COLUMNS + j < strText.length ; j++) {
				Button button = new Button(this);
				button.setText(strText[i * COLUMNS + j]);
				button.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						LayoutInflater inflate = (LayoutInflater)GraphResult.
							this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
						View layout = inflate.inflate(R.layout.axispopup, 
								(ViewGroup)findViewById(R.id.linear_popup));
						popup = new PopupWindow(layout, 50, 100, true);
						Button btnX = (Button)layout.findViewById(R.id.button_X);
						btnX.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								popup.dismiss();
							}
						});
						Button btnY = (Button)layout.findViewById(R.id.button_Y);
						btnY.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								popup.dismiss();
							}
						});
						Rect r = new Rect();
						Point offset = new Point();
						v.getGlobalVisibleRect(r, offset);
						popup.showAtLocation(layout, Gravity.NO_GRAVITY, 
								r.right, r.top);
					}
				});
				tableRow.addView(button);
			}
			layout.addView(tableRow);
		}
		
	}
}
